<?php

namespace Home\Model;

use Think\Model;

class BlogModel extends Model {

	/**
	 * 保存数据
	 *
	 * @param unknown $data
	 * @return string
	 */
	public function saveData($data, $id) {
		if ($id) {
			// 编辑
			if ($data ['title']) {
				$_data ['title'] = $data ['title'];
			}
			if ($data ['context']) {
				$_data ['context'] = $data ['context'];
			}
			$_data ['updatetime'] = date ( 'Y-m-d H:i:s' );
			$_data ['updateip'] = get_user_ip ();
			$rs = $this->where ( 'id=' . intval ( $id ) )->save ( $_data );
			if ($rs) {
				$result ['status'] = 1;
				$result ['msg'] = '编辑成功';
				$result ['id'] = $rs;
			} else {
				$result ['status'] = 0;
				$result ['msg'] = '没有任何操作';
			}
		} else {
			// 新增
			if (! $data ['title']) {
				$result ['status'] = 0;
				$result ['msg'] = '标题不能空';
			} elseif (! $data ['context']) {
				$result ['status'] = 0;
				$result ['msg'] = '内容不能空';
			} else {
				$data ['uid'] = get_cookie ( 'uid' );
				$data ['updatetime'] = $data ['createtime'] = date ( 'Y-m-d H:i:s' );
				$data ['createip'] = get_user_ip ();
				$rs = $this->add ( $data );
				if ($rs) {
					$result ['status'] = 1;
					$result ['msg'] = $rs;
					$result ['id'] = $rs;
				} else {
					$result ['status'] = 0;
					$result ['msg'] = '添加失败';
				}
			}
		}
		return $result;
	}

	/**
	 * 搜索列表
	 *
	 * @param number $where
	 * @param number $page
	 * @param string $order
	 * @param intval $num
	 *        	每页显示条数
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = 12) {
		$page = intval ( $page ) < 1 ? 1 : $page;
		$result ['list'] = $this->field ( 'id' )->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		foreach ( $result ['list'] as $k => $v ) {
			$result ['list'] [$k] = $this->getInfo ( $v ['id'] );
		}
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		return $result;
	}

	/**
	 * 获取单条信息
	 *
	 * @param unknown $id
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, mixed, unknown, string, object>
	 */
	public function getInfo($id) {
		$where = 'id=' . intval ( $id );
		$result = $this->where ( $where )->find ();
		$result ['url'] = '/?a=blogshow&id=' . $result ['id'];
		$result ['text200'] = cut_str ( strip_tags ( $result ['intro'] ), 200, '' );
		$result ['updatedata'] = date ( 'Y-m-d', strtotime ( $result ['updatetime'] ) );
		return $result;
	}

	/**
	 * 删除操作(non-PHPdoc)
	 *
	 * @see \Think\Model::delete()
	 */
	public function delete($id) {
		$result ['msg'] = '删除失败';
		$result ['status'] = 0;
		if ($id) {
			// 检查是否有子信息

			// 删除
			$rs = M ( 'site' )->where ( 'id=' . intval ( $id ) )->delete ();
			;

			$result ['msg'] = '删除成功';
			$result ['status'] = 1;
		} else {
			$result ['msg'] = '删除失败';
			$result ['status'] = 0;
		}
		return $result;
	}

	/**
	 * 获取用户的日志
	 *
	 * @param unknown $uid
	 * @param number $num
	 * @param int $except
	 *        	排除日志的ID
	 * @return Ambigous <\Home\Model\unknown, number, string>
	 */
	public function getUserBlog($uid, $num = 10, $except = '') {
		$uid = intval ( $uid ) ? $uid : get_cookie ( 'uid' );
		$where = 'uid=' . intval ( $uid );
		if ($except) {
			$where .= ' and id!=' . intval ( $except );
		}
		$page = intval ( $_REQUEST ['page'] );
		$order = 'updatetime desc';
		$result = $this->search ( $where, $page, $order, $num );
		return $result;
	}

	/**
	 * 获取热门日志
	 *
	 * @param unknown $uid
	 * @param number $num
	 * @param int $except
	 *        	排除日志的ID
	 * @return Ambigous <\Home\Model\unknown, number, string>
	 */
	public function getHotBlog($uid, $num = 10, $except = '') {
		$where = '1';
		if ($uid) {
			$where .= ' and uid=' . intval ( $uid );
		}
		if ($except) {
			$where .= ' and id!=' . intval ( $except );
		}
		$num = intval ( $num );
		$order = 'viewnum desc';
		$result = $this->search ( $where, 1, $order, $num );
		return $result;
	}
}
